import plotly.express as px
from pywebio.output import *
from common.config import yawConfig as Config

import plotly.graph_objects as go
import pandas as pd
from back.web.styleConfig import  tableStyle

import numpy as np
def yaw_onlInitialData():
    # with use_scope("table_scope", clear=True):
    #     put_table(pd.read_csv(Config.onlInitialDataAttr, header=None).values.tolist())
    #     put_html(tableStyle)
    X = pd.read_csv(Config.onlInitialData)
    X = X if len(X) < Config.displayMaxPoints else X.sample(Config.displayMaxPoints)
    fig = px.scatter_3d(X,
                        x=Config.WindSpeed, y=Config.WindDirection, z=Config.ActPower,
                        size=Config.WindSpeed,
                        size_max=Config.size_max,
                        opacity=Config.opacity,
                        color=Config.ActPower)
    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0), height=Config.scatter_3d_height)
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))
def yaw_onlCleanProcess():
    # with use_scope("table_scope", clear=True):
    #     put_table(pd.read_csv(Config.onlCleanAttr, header=None).values.tolist())
    #     put_html(tableStyle)

    X = pd.read_csv(Config.onlCleanProcess)
    X = X if len(X) < Config.displayMaxPoints else X.sample(Config.displayMaxPoints)
    fig = px.scatter_3d(X,
                        x=Config.WindSpeed, y=Config.WindDirection, z=Config.ActPower,
                        size=Config.WindSpeed,
                        size_max=Config.size_max,
                        opacity=Config.opacity,
                        color=Config.ActPower)
    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0), height=Config.scatter_3d_height)
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))
def yaw_onlGroupProcess():
    # with use_scope("table_scope", clear=True):
    #     put_table(pd.read_csv(Config.onlGroupAttr, header=None).values.tolist())
    #     put_html(tableStyle)

    fig = px.scatter_3d(pd.read_csv(Config.onlGroupProcess),
                        x=Config.WindSpeed, y=Config.WindDirection, z=Config.ActPower,
                        size=Config.WindSpeed,
                        size_max=Config.size_max,
                        opacity=Config.opacity,
                        color=Config.ActPower)
    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0), height=Config.scatter_3d_height)
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))
def yaw_onlSmoothProcess():
    # with use_scope("table_scope", clear=True):
    #     put_table(pd.read_csv(Config.onlSmoothAttr, header=None).values.tolist())
    #     put_html(tableStyle)

    fig = px.scatter_3d(pd.read_csv(Config.onlSmoothProcess),
                        x=Config.WindSpeed, y=Config.WindDirection, z=Config.ActPower,
                        size=Config.WindSpeed,
                        size_max=Config.size_max,
                        opacity=Config.opacity,
                        color=Config.ActPower)
    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0), height=Config.scatter_3d_height)
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))

def yaw_onlFusionProcess():
    # with use_scope("table_scope", clear=True):
    #     put_table(pd.read_csv(Config.onlFusionAttr, header=None).values.tolist())
    #     put_html(tableStyle)

    X = pd.read_csv(Config.onlFusionProcess)
    X = X if len(X) < Config.displayMaxPoints else X.sample(Config.displayMaxPoints)
    fig = px.scatter_3d(X,
                        x=Config.WindSpeed, y=Config.WindDirection, z=Config.ActPower,
                        size=Config.WindSpeed,
                        size_max=Config.size_max,
                        opacity=Config.opacity,
                        color=Config.ActPower)
    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0), height=Config.scatter_3d_height)
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))

def yaw_onlSurfaceProcess():
    # with use_scope("table_scope", clear=True):
    #     put_table(pd.read_csv(Config.onlSurfaceAttr, header=None).values.tolist())
    #     put_html(tableStyle)
    # import time
    # while True:
    #     with use_scope('result', clear=True):
    #         fig = px.scatter_3d(pd.read_csv(Config.onlSurfaceProcess),
    #                         x=Config.WindSpeed, y=Config.WindDirection, z=Config.ActPower,
    #                         size=Config.WindSpeed,
    #                         size_max=Config.size_max,
    #                         opacity=Config.opacity,
    #                         color=Config.ActPower)
    #         fig.update_layout(margin=dict(l=0, r=0, b=0, t=0), height=Config.scatter_3d_height)

    #         put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))
    #     time.sleep(3)
    fig = px.scatter_3d(pd.read_csv(Config.onlSurfaceProcess),
                    x=Config.WindSpeed, y=Config.WindDirection, z=Config.ActPower,
                    size=Config.WindSpeed,
                    size_max=Config.size_max,
                    opacity=Config.opacity,
                    color=Config.ActPower)
    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0), height=Config.scatter_3d_height)

    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))


def yaw_onlSurfaceAxes():
    # with use_scope("table_scope", clear=True):
    #     put_table(pd.read_csv(Config.onlSurfaceAttr, header=None).values.tolist())
    #     put_html(tableStyle)

    X = pd.read_csv(Config.onlSurfaceProcess)
    x, y, z = X[Config.WindSpeed].values.tolist(), X[Config.WindDirection].values.tolist(), X[
        Config.ActPower].values.tolist()
    fig = go.Figure(data=[go.Mesh3d(x=x,
                                    y=y,
                                    z=z,
                                    opacity=0.7,
                                    color='rgba(100,22,200,0.9)'
                                    )])
    fig.update_layout(scene=dict(
        xaxis_title=Config.WindSpeed,
        yaxis_title=Config.WindDirection,
        zaxis_title=Config.ActPower),
        # width=700,
        margin=dict(l=0, r=0, b=0, t=0), height=Config.scatter_3d_height)
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))
def yaw_onlSurfacePlotProcess():
    # with use_scope("table_scope", clear=True):
    #     put_table(pd.read_csv(Config.onlSurfaceAttr, header=None).values.tolist())
    #     put_html(tableStyle)

    X = pd.read_csv(Config.onlSurfaceProcess)
    z = X.values
    sh_0, sh_1 = z.shape
    x, y = np.linspace(-Config.misalignment, Config.misalignment, sh_0), np.linspace(1, Config.windSpeedFilter_max,
                                                                                     sh_1)
    fig = go.Figure(data=[go.Surface(z=z, x=x, y=y)])
    fig.update_traces(contours_z=dict(show=True, usecolormap=True,
                                      highlightcolor="limegreen", project_z=True))
    fig.update_layout(height=Config.scatter_3d_height,
                      margin=dict(l=0, r=0, b=0, t=0))
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))
def yaw_onlLineRegress():
    # with use_scope("table_scope", clear=True):
    #     put_table(pd.read_csv(Config.onlLineRegressAttr, header=None).values.tolist())
    #     put_html(tableStyle)

    X = pd.read_csv(Config.onlLineRegress)
    fig = px.line(X, x=Config.WindDirection, y=Config.WindSpeed, title='偏航误差曲线',
                  range_x=[-Config.misalignment, Config.misalignment])
    fig.update_layout(title='偏航误差识别', xaxis_title='风向偏差',yaxis_title='风速[m/s]')
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))

def yaw_lineCompareProcess():
    # with use_scope("table_scope", clear=True):
    #     put_table(pd.read_csv(Config.lineCompareAttr, header=None).values.tolist())
    #     put_html(tableStyle)

    X = pd.read_csv(Config.lineCompareProcess)
    columns = X.columns
    if len(columns) > 2:
        fig = px.line(X, x=columns[0], y=columns[1], color=columns[2], title='',
                      range_x=[-Config.misalignment+12, Config.misalignment-12])
    else:
        fig = px.line(X, x=columns[0], y=columns[1], title='',
                      range_x=[-Config.misalignment+12, Config.misalignment-12])
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))

def yaw_powerCompareProcess():
    # with use_scope("table_scope", clear=True):
    #     put_table(pd.read_csv(Config.powerCompareAttr, header=None).values.tolist())
    #     put_html(tableStyle)

    X = pd.read_csv(Config.powerCompareProcess)
    columns = X.columns
    if len(columns) > 2:
        fig = px.line(X, x=columns[0], y=columns[1], color=columns[2], title='',
                      range_x=[0, Config.misalignment])
    else:
        fig = px.line(X, x=columns[0], y=columns[1], title='',
                      range_x=[0, Config.misalignment])
    put_html(fig.to_html(include_plotlyjs="require", full_html=False,config={'displayModeBar':False}))

from pywebio import *
if __name__ == '__main__':
    # start_server(list, port=8000, debug=False, host='::')
    start_server(yaw_powerCompareProcess, port=9999, debug=False)